package ru.st.selenium.applogic1;

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Capabilities;
import ru.stqa.selenium.factory.WebDriverFactory;
import ru.stqa.selenium.factory.WebDriverFactoryMode;
import ru.st.selenium.applogic.ApplicationManager;
import ru.st.selenium.applogic1.ApplicationManager1;
import ru.st.selenium.util.PropertyLoader;
import ru.st.selenium.applogic.FilmHelper;
import ru.st.selenium.applogic.NavigationHelper;
import ru.st.selenium.applogic.UserHelper;

public class ApplicationManager1 implements ApplicationManager {

	private UserHelper userHelper;
	private FilmHelper filmHelper;
	private NavigationHelper navHelper;

	protected static String gridHubUrl;
	protected static String baseUrl;
	protected static Capabilities capabilities;
	protected ApplicationManager app;
	protected WebDriver driver;

	public ApplicationManager1() throws IOException {

		baseUrl = PropertyLoader.loadProperty("site.url");
		gridHubUrl = PropertyLoader.loadProperty("grid.url");
		if ("".equals(gridHubUrl)) {
			gridHubUrl = null;
		}

		capabilities = PropertyLoader.loadCapabilities();
		WebDriverFactory.setMode(WebDriverFactoryMode.THREADLOCAL_SINGLETON);
		driver = WebDriverFactory.getDriver(gridHubUrl, capabilities);

		userHelper = new UserHelper1(this);
		filmHelper = new FilmHelper1(this);
		navHelper = new NavigationHelper1(this);

		getNavigationHelper().openMainPage();
	}

	@Override
	public UserHelper getUserHelper() {
		return userHelper;
	}

	@Override
	public FilmHelper getFilmHelper() {
		return filmHelper;
	}

	@Override
	public NavigationHelper getNavigationHelper() {
		return navHelper;
	}

	protected WebDriver getWebDriver() {
		return driver;
	}

	protected String getBaseUrl() {
		return baseUrl;
	}

	@Override
	public void stop() {
		if (driver != null) {
			driver.quit();
		}
	}
}
